using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Utilities.Menu;
using Services;
using WAS.Resources;
using Utilities.Menu.UIElements;


namespace WAS.Menu
{
    using Game = Microsoft.Xna.Framework.Game;

    public struct Language
    {
        public Texture2D Flag;
        public string Tag;
    }

    public class LanguageSelection : DrawableGameComponent
    {
        SpriteBatch sprite_batch;
        Fader fader;
        Texture2D bg, title_bg;
        FlagButton[] flag_buttons;
        Matrix screen_transform;
        SoundEffect click;
        SpriteFont font_30;

        public LanguageSelection(Game game, Language[] languages)
            : base(game)
        {
            flag_buttons = new FlagButton[languages.Length];

            int starting_y = 130;
            for (int i = 0; i < languages.Length; i++)
            {
                var flag = languages[i].Flag;
                flag_buttons[i] = new FlagButton { Flag = flag, CollisionBox = new Rectangle(Game.GraphicsDevice.Viewport.Width / 2 - flag.Width / 2, starting_y + (flag.Height + 20) * i, flag.Width, flag.Height), Tag = languages[i].Tag};
            }

            game.Components.Add(new InputManager(game));
        }

        public override void Initialize()
        {
            sprite_batch = (SpriteBatch)Game.Services.GetService(typeof(SpriteBatch));

            fader = new Fader(Game, FadeType.In, 0.0f, 0.0f, 1.0f);
            Game.Components.Add(fader);

            //screen_transform = Game.ScreenTransformMatrix();
            screen_transform = Matrix.CreateScale(GraphicsDevice.Viewport.Width / SpaceBattleGame.DEFAULT_SCR_WIDTH, GraphicsDevice.Viewport.Height / SpaceBattleGame.DEFAULT_SCR_HEIGHT, 1.0f);
            var input_service = Game.Services.GetService(typeof(IInputManager)) as IInputManager;
            input_service.OnTap += new InputManager.Tap(on_tap);

            bg = Game.Content.Load<Texture2D>(@"Menu\bg");
            title_bg = Game.Content.Load<Texture2D>(@"Menu\title_bg");
            click = Game.Content.Load<SoundEffect>("Audio/button");
            font_30 = Game.Content.Load<SpriteFont>("Fonts/OCRAExtended30");

            

            base.Initialize();
        }

        private void on_tap(Vector2 tap_position)
        {
            var t_p = tap_position.ToPoint(screen_transform);

            for (int i = 0; i < flag_buttons.Length; i++)
            {
                if (flag_buttons[i].CollisionBox.Contains(t_p))
                {
                    fader.FadeType = FadeType.Out;
                    Strings.Culture = new System.Globalization.CultureInfo(flag_buttons[i].Tag);
                    click.Play();
                    break;
                }
            }
        }

        public override void Update(GameTime gameTime)
        {
            if (fader.FadeType == FadeType.Out && fader.Alpha <= 0.0f)
            {
                StateManager.SelectMenu(Game);
            }
            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            sprite_batch.Begin(SpriteSortMode.Deferred, null, null, null, null, null, screen_transform);

            sprite_batch.Draw(bg, Vector2.Zero, Color.White);

            var sm = font_30.MeasureString(Strings.Language);

            sprite_batch.Draw(title_bg, new Vector2(Game.GraphicsDevice.Viewport.Width / 2.0f - title_bg.Width / 2.0f, 50 - (title_bg.Height - sm.Y) / 2.0f), Color.White * fader.Alpha);

            sprite_batch.DrawString(font_30, Strings.Language, new Vector2(Game.GraphicsDevice.Viewport.Width / 2.0f - sm.X / 2.0f, 50) + Vector2.One * 3.0f, Color.Black * fader.Alpha);
            sprite_batch.DrawString(font_30, Strings.Language, new Vector2(Game.GraphicsDevice.Viewport.Width / 2.0f - sm.X / 2.0f, 50), Color.White * fader.Alpha);

            for (int i = 0; i < flag_buttons.Length; i++)
            {
                sprite_batch.Draw(flag_buttons[i].Flag, flag_buttons[i].CollisionBox, Color.White * fader.Alpha);
            }

            sprite_batch.End();

            base.Draw(gameTime);
        }

        protected override void Dispose(bool disposing)
        {
            Game.Services.RemoveService(typeof(InputManager));

            base.Dispose(disposing);
        }

        private struct FlagButton
        {
            public Texture2D Flag;
            public Rectangle CollisionBox;
            public string Tag;
        }
    }
}
